/**
 * Created by dongp on 2016/5/7.
 */

/**
 * Created by dongp on 2016/5/6.
 */

$(function(){
    validAccount("#account","#errorAccount");
    validPasword("#password","#errorPassword");
    validCode("#vCode","#errorCode");

    // 提交前验证
    $("#formId").on("submit",function(){
        var flag = true;
        var account = $("#account").val();
        var password = $("#password").val();
        var vCode = $("#vCode").val();

        if(isEmpty(account)) {
            $("#errorAccount").html("输入内容不能为空");
            $("#errorAccount").show();
            flag = false;
        }

        if(isEmpty(password)) {
            $("#errorPassword").html("输入内容不能为空");
            $("#errorPassword").show();
            flag = false;
        }else {
            if(!validLen(password)) {
                $("#errorPassword").html("请输入3至16个字符");
                $("#errorPassword").show();
                flag = false;
            }
        }

        if(isEmpty(vCode)) {
            $("#errorCode").html("验证码不能为空");
            $("#errorCode").show();
            flag = false;
        }else {
            if(vCode.trim().length != 4) {
                $("#errorCode").html("请输入正确的验证码");
                $("#errorCode").show();
                flag = false;
            }
        }

        if(!flag) {
            return false;
        }else {
            return true;
        }
    });

});

//验证账号
function validAccount(selectId,errorArea){
    var $selectId = $(selectId);
    var $errorArea = $(errorArea);
    $selectId.on("focusout",function(){
        var text = $selectId.val();
        if(isEmpty(text)) {
            $errorArea.html("输入不能为空");
            $errorArea.show();
            return false;
        }
    });
    $selectId.on("focusin",function(){
        $errorArea.hide();
        $("#errorMsg").hide();
    });
}

//验证密码
function validPasword(selectId,errorArea){
    var $selectId = $(selectId);
    var $errorArea = $(errorArea);
    $selectId.on("focusout",function(){
        var text = $selectId.val();
        if(isEmpty(text)) {
            $errorArea.html("输入不能为空");
            $errorArea.show();
            return false;
        }

        if(!validLen(text)) {
            $errorArea.html("请输入3至16个字符");
            $errorArea.show();
            return false;
        }
    });
    $selectId.on("focusin",function(){
        $errorArea.hide();
        $("#errorMsg").hide();
    });
}

//验证码
function validCode(selectId,errorArea){
    var $selectId = $(selectId);
    var $errorArea = $(errorArea);
    $selectId.on("focusout",function(){
        var text = $selectId.val();
        if(isEmpty(text)) {
            $errorArea.html("验证码不能为空");
            $errorArea.show();
            return false;
        }
        if(text.trim().length != 4 ) {
            $errorArea.html("请输入正确验证码");
            $errorArea.show();
            return false;
        }
    });
    $selectId.on("focusin",function(){
        $errorArea.hide();
        $("#errorMsg").hide();
    });
}


function isEmpty(text) {
    if(!text) {
        return  true;
    }

    if(text.trim().length < 1) {
        return true;
    }

    return false;
}

function validLen(text) {
    if(text.trim().length < 3 || text.trim().length > 16 ) {
        return false;
    }

    return true;
}

function toggleValidCode(){
    var imgSrc = $("#vCodeId").attr("src");
    var index = imgSrc.indexOf("?") > 0 ? imgSrc.indexOf("?") : imgSrc.length;
    imgSrc = imgSrc.substr(0,index)+"?"+new Date().getTime();
    $("#vCodeId").attr("src",imgSrc);
}
